package com.lagou.app;

import com.lagou.entity.Phone;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 手机类测试
 */


public class PhoneTest {

    public static void main(String[] args) {
        //1.获取QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        //2.编写sql
        try {
            // 需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
            String sql1 = "select * from phone where price > ? and prodate < ?";
            Object[] param = {2000,"2019-01-01"};
            List<Phone> phone1 = qr.query(sql1,new BeanListHandler<Phone>(Phone.class),param);
            //3.创建集合保存手机信息
            List<Phone> phone1List = new ArrayList<>();
            for(Phone p:phone1){
                phone1List.add(p);
            }
            System.out.println("价格高于2000元，生产日期是2019年之前的所有手机");
            for(Phone ph : phone1List){
                System.out.println(ph.toString());
            }

            System.out.println("----------------------------------------------------------------------");

            // 需求2:  查询所有颜色是白色的手机信息
            String sql2 = "select * from phone where color = ?";
            List<Phone> phone2 = qr.query(sql2,new BeanListHandler<Phone>(Phone.class),"白色");
            //3.创建集合保存手机信息
            List<Phone> phone2List = new ArrayList<>();
            for(Phone p:phone2){
                phone2List.add(p);
            }
            System.out.println("所有颜色是白色的手机信息");
            for(Phone ph : phone2List){
                System.out.println(ph.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
